package com.shiku.imserver.message.processor;

import com.shiku.imserver.common.AbstractService;
import com.shiku.imserver.common.annotation.MessageCommandAnnotation;
import com.shiku.imserver.common.constant.IMLoggers;
import com.shiku.imserver.common.hander.AbstractMessageHandler;
import com.shiku.imserver.common.hander.IMessageHandler;
import com.shiku.imserver.common.message.PacketVO;
import com.shiku.imserver.common.packets.ImPacket;
import com.shiku.imserver.common.scanner.ClassScanner;
import com.shiku.imserver.service.IMBeanUtils;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.tio.core.ChannelContext;

public class MessageProcess extends AbstractService {
    public static final Logger logger = IMLoggers.serverLog;
    public ClassScanner classScanner = new ClassScanner();
    public String[] fileNames;
    protected Map<Short, IMessageHandler> handlers = new HashMap();

    public void addHandler(short httpCode, IMessageHandler handler) {
        this.handlers.put(Short.valueOf(httpCode), handler);
    }

    public PacketVO dispatch(ImPacket packet, ChannelContext channelContext) {
        PacketVO result = null;
        try {
            short cmd = packet.getCommand();
            IMessageHandler handler = this.handlers.get(Short.valueOf(cmd));
            if (handler == null) {
                if (7 != cmd) {
                    logger.error("handler is null userId  {}  cmd {} ", channelContext.userid, Short.valueOf(packet.getCommand()));
                }
                return null;
            }
            Method method = handler.getMessageHandler(cmd);
            if (method == null) {
                logger.error("handler is null userId  {}  cmd {} ", channelContext.userid, Short.valueOf(packet.getCommand()));
                return null;
            }
            method.setAccessible(true);
            Object object = method.invoke(handler, packet, channelContext);
            if (object != null) {
                result = (PacketVO) object;
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void loadPackage(String namespace, String ext) throws Exception {
        if (this.fileNames == null) {
            this.fileNames = this.classScanner.scannerPackage(namespace, ext);
        }
        if (this.fileNames != null) {
            String[] strArr = this.fileNames;
            for (String fileName : strArr) {
                Class<?> messageClass = Class.forName(namespace + "." + ((Object) fileName.subSequence(0, fileName.length() - ext.length())));
                logger.info("handler load: " + messageClass);
                IMessageHandler iMessageHandler = getMessageHandler(messageClass);
                ((AbstractMessageHandler) iMessageHandler).init();
                Method[] methods = messageClass.getMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(MessageCommandAnnotation.class)) {
                        logger.info("handler method name  " + method.getName());
                        MessageCommandAnnotation messageCommandAnnotation = method.getAnnotation(MessageCommandAnnotation.class);
                        if (messageCommandAnnotation != null) {
                            addHandler(messageCommandAnnotation.command(), iMessageHandler);
                        } else {
                            logger.info("handler is null  " + messageClass);
                        }
                    }
                }
            }
            logger.info("handler size is " + this.handlers.size());
        }
    }

    public final IMessageHandler getMessageHandler(Class<?> classes) {
        if (classes == null) {
            return null;
        }
        try {
            return (IMessageHandler)classes.newInstance();
        } catch (Exception e) {
            logger.error("getMessageHandler - classes=" + classes.getName() + ". ", e);
            return null;
        }
    }

    public boolean initialize() {
        try {
            loadPackage(IMBeanUtils.getImconfig().getMessageHandlerNameSpace(), ".class");
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
